package com.feng.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.feng.entity.Publish;
import com.feng.util.db.DBHelp;
import com.feng.util.db.Page;
import com.feng.util.db.RowMapper;

public class PublishDao {

	DBHelp<Publish> db = new DBHelp<Publish>();

	public Page<Publish> findByPageNum(int pageNum) {
		Page<Publish> page = new Page<Publish>(getTotle(), pageNum);
		String sql = "select id,pubname,createtime from t_publish order by createtime limit ?,?";
		List<Publish> list = db.executeQueryToList(sql, new PublishRowMapper(), page.getStart(),page.getPageSize());
		page.setList(list);
		return page;
	}
	public int getTotle() {
		String sql = "select count(*) from t_publish ";
		return  (int) db.executeQuery(sql);
	}
	private class PublishRowMapper implements RowMapper<Publish>{

		public Publish mapRow(ResultSet rs) throws SQLException {
			Publish p = new Publish();
			p.setId(rs.getString("id"));
			p.setPubname(rs.getString("pubname"));
			p.setCreateTime(rs.getString("createtime"));
			return p;
		}
		
	}
	public void create(String puname,String id) {
		String sql = "insert into t_publish (id,pubname) values (?,?)";
		db.executeSQL(sql, id,puname);
	}
	public Publish findById(String id) {
		String sql = "select id,pubname,createtime from t_publish where id=?";
		return db.executeQueryToObject(sql, new PublishRowMapper(), id);
	}
	public void save(String id, String pubname) {
		String sql = "update t_publish set pubname=?where id=?";
		db.executeSQL(sql, pubname,id);
	}
	public void del(String id) {
		String sql = "delete from t_publish where id=?";
		db.executeSQL(sql, id);
	}
	public List<Publish> findAll() {
		String sql = "select id,pubname,createtime from t_publish";
		return db.executeQueryToList(sql, new PublishRowMapper());
	}
}
